#include <stdio.h>

#include "flake.h"

/*initialize the flake's position
 * */
flake::flake() {
	init();
}

flake::~flake() {

}

void flake::init() {
	reset();
	pos.y = (float) rand() / RAND_MAX;
	pos.y = pos.y * 2 - 1;
}

/*
 * reset the flake's position
 * */
void flake::reset() {
	pos.x = (float) rand() / RAND_MAX;
	pos.y = (float) rand() / RAND_MAX;
	pos.z = (float) rand() / RAND_MAX;

	pos.x = pos.x * 2 - 1;
	pos.z = pos.z * 2 - 1;

	vel.x = 0.0;
	vel.y = -0.02;
	vel.z = 0.0;

	size = 0.02;
}

/*
 * calculate the next position , with wind and turbulance
 * */
void flake::nextPos(float rscale, vector3f* wind) {
	pos = vel + pos + *wind;

	if (pos.x < -1.0)
		pos.x = 1.0;
	else if (pos.x > 1.0)
		pos.x = -1.0;

	if (pos.z < -1.0)
		pos.z = 1.0;
	else if (pos.z > 1.0)
		pos.z = -1.0;

	vel.x += (((float) (rand()) / RAND_MAX) - 1.0) * rscale;

	vel.z += (((float) (rand()) / RAND_MAX) - 1.0) * rscale;
}

void flake::plot() {
	glVertex3f(pos.x, pos.y, pos.z);
}
